define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, NewsendDetail, $ionicSlideBoxDelegate, GoPage, GoDialog, GoShare,GoWapAuth, $window, $timeout, $stateParams, $ionicModal, Auth, $interval, $ionicPopup) {
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.input = new Object();
//			$scope.img = "";
//			$scope.text = "";
			$scope.isshow = true;
			$scope.set_time = false;
			$scope.goods_id = $stateParams.goods_id;
			$scope.product_id = $stateParams.product_id;
			$scope.act_id = $stateParams.act_id;
			$scope.init_data();
		}, false);
		
		$scope.$on('$ionicView.afterLeave', function() {
			$scope.closeGuidShare();
		}, false);
		
		$scope.$on('$ionicView.beforeLeave',function(){
			if($scope.timer){
				$interval.cancel($scope.timer);
			}
		})
		
		//初始化页面
		$scope.init_data = function() {
			NewsendDetail.product_detail($scope.act_id,$scope.goods_id,$scope.product_id, function(data) {
				if(!data.success){
					GoDialog.alert("提示",data.msg,function(){
						GoPage.go_back();
					})
					return;
				}
				console.log(data)
				if(data.result) {
					$scope.datas = data.result;
					$scope.detail = data.result.goods;
					$scope.init_img(data.process_time);
					$scope.init_time(data.result, data.process_time);
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
					$scope.go_share_wx();
				}
			});
		}
		
		/************提交表单***************/
		var myPopup_answer;
		$scope.get_paper = function() {
			NewsendDetail.get_paper($scope.detail.qsid, function(data) {
				console.log(data);
				if(data.success == true) {
					$scope.input.answer = data.result;
					myPopup_answer = $ionicPopup.show({
						templateUrl: 'templates/modal/answer_list.html',
						title: "测试问卷",
						scope: $scope,
						isConfirm: true,
						cssClass:"activityThree",
						buttons: [{
							text: "取消关闭",
							onTap: function(e) {

							}
						}, {
							text: "提交答卷",
							type: "buttonOrange",
							onTap: function(e) {
								var asked = new Array();
								for(var i = 0; i < $scope.input.answer.length; i++) {
									asked[$scope.input.answer[i].qid] = {
										'qid': $scope.input.answer[i].qid,
										'answer': $scope.input.answer[i].checked
									}
								}
								if(!$scope.input.agree) {
									e.preventDefault();
									GoDialog.toast('请同意申明！');
									return;
								}
								NewsendDetail.send_apply($scope.act_id, $scope.file_id, $scope.filename, asked, function(data) {
									GoDialog.alert("提示",data.msg);
									if(!data.success){
										return;
									}
//									GoDialog.alert("提示","您的申请已成功提交，等待系统审核！");
									$scope.isshow = false;
									$scope.btn_name = '等待审核';
									$scope.datas.apply_members_count = data.result.apply_members_count;//更新申请人数
								});
							}
						}]
					})
				} else {
					GoDialog.toast(data.msg);
				}
			})
		}
		
		$scope.selected = function(data, index) {
			var checked = new Array();
			for(var i = 0; i < data.answers.length; i++) {
				if(data.answers[i].check == true) {
					checked[checked.length] = data.answers[i].item_latter;
				}
			}
			data.checked = new Array();
			data.checked = checked;
		}
		/************提交表单***************/
		
		$scope.init_time = function(data, now_time) {
			if(now_time < data.start_time) {
				$scope.isshow = false;
				$scope.btn_name = '未开始';
				$scope.act_time_name = '距开始';
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.start_time - now_time;
			} else {
				$scope.act_time_name = '距结束';
				if($scope.detail.store <= 0) {
					$scope.btn_name = '已送完';
					$scope.isshow = false;
				}
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.end_time - now_time;

			}
			data.formate_time = $scope.formate_time(data.now_time);
			$scope.set_timer(now_time);
		};
		$scope.set_timer = function(now_time) {
			if(!$scope.set_time) {
				$scope.set_time = true;
			} else {
				return;
			}
			$interval(function() {
				if($scope.datas.now_time != undefined) {
					$scope.datas.now_time = $scope.datas.now_time - 1;
					if($scope.datas.now_time <= 0) {
						if(now_time >= $scope.datas.start_time) {
							$scope.datas.now_time = $scope.datas.end_time - now_time;
							$scope.act_time_name = '距结束';
						} else {
							$scope.datas.now_time = $scope.datas.start_time - now_time;
							$scope.act_time_name = '距开始';
							if($scope.detail.store <= 0) {
								$scope.btn_name = '已送完';
								$scope.isshow = false;
							} else {
								$scope.isshow = true;
							}
						}
					}
				} else {
					if(now_time > $scope.datas.start_time) {
						$scope.datas.now_time = $scope.datas.end_time - now_time;
						$scope.act_time_name = '距结束';
					} else {
						$scope.datas.now_time = $scope.datas.start_time - now_time;
						$scope.act_time_name = '距开始';
						if($scope.detail.store <= 0) {
							$scope.btn_name = '已送完';
							$scope.isshow = false;
						} else {
							$scope.isshow = true;
						}
					}
				}
				if(now_time > $scope.datas.apply_end_time) {
					$scope.isshow = false;
					$scope.btn_name = '申请结束';
				}
				now_time++;
				$scope.datas.formate_time = $scope.formate_time($scope.datas.now_time);
			}, 1000)
		};
		//计算时间
		$scope.formate_time = function(time) {
			var timeArr = [0,0,0,0];
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
//				return day + '天' + hour + '小时' + min + '分' + second + '秒';
				timeArr[0] = day;
				timeArr[1] = hour;
				timeArr[2] = min;
				timeArr[3] = second;
			} else {
				$scope.isshow = false;
				$scope.btn_name = '已结束';
//				return '0天0小时0分0秒';
			}
			return timeArr;
		};
		
		$scope.init_img = function(now_time) {
			if(now_time >= $scope.datas.start_time && now_time <= $scope.datas.aGoods.activity_info.end_time &&$scope.datas.activity.act_open == 'true') {
				if($scope.datas.aGoods.nostore_sell || $scope.detail.store > 0) {
					if(!$scope.datas.is_apply && now_time <= $scope.datas.activity.apply_end_time) {
						$scope.btn_name = '立即申请'
					} else {
						if($scope.datas.is_apply) {
//							$scope.img = 'img/waiting.png';
							GoDialog.alert("提示","您的申请已成功提交，等待系统审核！");
							$scope.isshow = false;
							$scope.btn_name = '等待审核';
						} else {
							$scope.btn_name = '申请已结束';
							$scope.isshow = false;
//							$scope.text = "该申请已结束";
							GoDialog.alert("提示","该申请已结束");
						}
					}
				} else {
					$scope.btn_name = '已送完';
					$scope.isshow = false;
				}
			} else if(now_time < $scope.datas.activity.start_time) {
				$scope.btn_name = '未开始';
				$scope.isshow = false;
			} else {
				if($scope.datas.order) {
					if($scope.datas.order.order_id){
//						$scope.text = "恭喜！您已获得免费赠送资格！";
						GoDialog.alert("提示","恭喜！您已获得免费赠送资格！");
					}else{
//						$scope.text = "很遗憾！您没能获得免费赠送资格！谢谢参与！";
						GoDialog.alert("提示","很遗憾！您没能获得免费赠送资格！谢谢参与！");
					}
					$scope.isshow = false;
					$scope.btn_name = '立即申请';
				} else if($scope.is_apply) {
//					$scope.text = "很遗憾！您没能获得免费赠送资格！谢谢参与！";
					GoDialog.alert("提示","很遗憾！您没能获得免费赠送资格！谢谢参与！");
					$scope.isshow = false;
				}
			}
		}
		
		//跳转到首页
		$scope.onshow_first = function() {
			GoPage.go_states("first", {"store_id": $scope.detail.store_id});
		};
		
		//跳转到商品详情
		$scope.onshow_productInfo = function() {
			GoPage.go_states("activity_info", {
				"goods_id": $scope.goods_id,
				"act_id": $scope.act_id,
				"product_id": $scope.product_id,
				"type":6
			});
		}
		
		//上拉跳转到商品详情
		$scope.isLoad = false;
		$scope.doRefreshMore = function(){
			$scope.isLoad = true;
			$scope.$broadcast('scroll.infiniteScrollComplete');
			$timeout(function() {
				$scope.isLoad = false;
			}, 3000);
			$scope.onshow_productInfo();
		}
		
		
		/***********显示《客服》**********/
		$ionicModal.fromTemplateUrl("templates/modal/customer_service.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_customer = modal;
		});
		//打开
		$scope.openModal_customer = function() {//客服
			NewsendDetail.contact_show($scope.detail.store_id, function(data) {
				if(!data||data.cusphone.length<1){
					GoDialog.alert("提示","店家暂时还没有设置客服哦");
					return;
				}
				$scope.customerList = data;
				$scope.modal_customer.show();
			})
		}
		//隐藏
		$scope.closeModal_customer = function() {
			$scope.modal_customer.hide();
		}
		//选择客服
		$scope.selectCustomer = function(index){
			$scope.customerList_index = index;
		}
		//联系客服
		$scope.do_tel = function() {
			$scope.closeModal_customer();
			if(!$scope.customerList_index) return;
			var param = $scope.customerList.cusphone[$scope.customerList_index];
			if(param.type == 2) {
				$window.location.href = "tel:" + param.value;
			} else if(param.type == 1) {
				if(param.isonline == '0') {
					GoPage.go_txcall3(param.id, param.code, param.vvckey);
				} else {
					GoPage.go_txcall2($scope.detail.store_id, $scope.detail.store_id, param.id, param.code, param.vvckey);
				}
			}
		}
		/***********显示《客服》**********/
		
		/***************活动规则**************/
		$ionicModal.fromTemplateUrl("templates/modal/activityRule.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_rule = modal;
		});
		//打开
		$scope.openModal_rule = function() {
			$scope.detail.rule = $scope.datas.rule;//字段转换一下
			$scope.modal_rule.show();
		}
		//隐藏
		$scope.closeModal_rule = function() {
			$scope.modal_rule.hide();
		}
		/***************活动规则**************/
		
		/***************分享**************/
		$ionicModal.fromTemplateUrl("templates/modal/keyshare_modal.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_share = modal;
		});
		//打开
		$scope.openModal_share = function() {
			if($scope.openGuidShare()) {
				$scope.modal_share.show();
			}
		}
		//隐藏
		$scope.closeModal_share = function() {
			$scope.modal_share.hide();
		}
		
		//分享（自定义每个页面的参数）
		$scope.go_share = function(index) {
			var param = new Object();
			if($scope.detail) {
				param.title = $scope.detail.name ? $scope.detail.name : undefined;
				param.desc = $scope.detail.brief ? $scope.detail.brief : undefined;
				param.thumb = $scope.detail.title_images ? $scope.detail.title_images[0].image : undefined;
				param.link = 'newsend_detail/'+$scope.act_id+'/'+$scope.goods_id +'/'+$scope.product_id;
			}
			GoShare.do_share(param, index);
		}
		
		//分享（自定义每个页面的参数）
		$scope.go_share_wx = function() {
			//分享打开
			GoWapAuth.execisoff(false);
			//设置分享
			var param = new Object();
			if($scope.detail) {
				param.title = $scope.detail.name ? $scope.detail.name : undefined;
				param.desc = $scope.detail.brief ? $scope.detail.brief : undefined;
				param.thumb = $scope.detail.title_images ? $scope.detail.title_images[0].image : undefined;
				param.link = 'activitythree/'+$scope.act_id+'/' +$scope.goods_id+'/'+$scope.product_id;
			}
			GoWapAuth.execshare(param);
		}
		/***************分享**************/
		
		
	}

	controller.$inject = ['$scope', 'NewsendDetail', '$ionicSlideBoxDelegate', 'GoPage', 'GoDialog', 'GoShare','GoWapAuth', '$window', '$timeout', '$stateParams', '$ionicModal', 'Auth', '$interval','$ionicPopup'];

	return app.registerController("newsend_detailctrl", controller);
});